$(document).ready(function () {
    /**
     * Cargamos el template de la creacion de las certificaciones
     */
    $(document).on('dblclick', 'div.uncertification', function () {
        if (appBosch.jsPermissions.crud.create === false) {
            /**
             * No tiene permisos para crear una certificacion :(
             * Hasta aqui llegamos
             */
            return;
        }

        var uncertification = $(this);
        /**
         * Guardamos la certificacion donde se dio clic
         */
        appBosch.certification = uncertification.data();
        createUsersAndWorkstations();
        appBosch.certification['wDays'] = parseInt(appBosch.workstations[appBosch.certification.workstation].days);
        appBosch.certification['wMaxDays'] = parseInt(appBosch.workstations[appBosch.certification.workstation].max_days);
        createStarAndEndDates();
        wndCreateCertification();
    });
    $('#createCertification').click(function () {
        var form = $('#wndCreateCertification form');
        var url = form.attr('action');
        var params = form.serialize();
        $('#loadingCreateCertitication').removeClass('hidden');
        $('#createCertification').addClass('hidden');
        $.getJSON(url, params, function (result) {
            appBosch.refreshDashboard = true;
            $("#wndCreateCertification").data('kendoWindow').close();
            if (result === false) {
                alert('Error: No se creo la certificación');
            }
        });
    });
});

function wndCreateCertification() {
    if (appBosch.wndCreateCertification === undefined) {
        $("#wndCreateCertification").removeClass('hidden');
        appBosch.wndCreateCertification = $("#wndCreateCertification").kendoWindow({
            width: 660,
            height: 300,
            title: 'Iniciar proceso de certificación',
            actions: [
                "Close"
            ],
            close: function () {
                //appBosch.iconRefresh.addClass('hidden');
                if (appBosch.refreshDashboard === true) {
                    $('#grid').data('kendoGrid').dataSource.read();
                }
            },
            open: function () {
                $('#loadingCreateCertitication').addClass('hidden');
                $('#createCertification').removeClass('hidden');
                appBosch.refreshDashboard = false;
                this.center();
            }
        }).data("kendoWindow");
    }
    else {
        appBosch.wndCreateCertification.open();
    }
}

function createUsersAndWorkstations() {
    if (appBosch.wndCreateCertificationUsers === undefined) {
        appBosch.wndCreateCertificationUsers = $("#wndCreateCertification input.users").kendoDropDownList({
            dataTextField: "text",
            dataValueField: "value",
            dataSource: appBosch.users
        }).data('kendoDropDownList');
    }
    if (appBosch.wndCreateCertificationWorkstations === undefined) {
        appBosch.wndCreateCertificationWorkstations = $("#wndCreateCertification input.workstations").kendoDropDownList({
            dataTextField: "name",
            dataValueField: "id",
            dataSource: appBosch.colDyn
        }).data('kendoDropDownList');
    }
    appBosch.wndCreateCertificationUsers.value(appBosch.certification.user);
    appBosch.wndCreateCertificationWorkstations.value(appBosch.certification.workstation);
}

function createStarAndEndDates() {
    var inputStart = $('input.cStart');
    var inputEnd = $('input.cEnd');
    var currentDate = new Date();
    var wDays = appBosch.certification['wDays'];
    var wMaxDays = appBosch.certification['wMaxDays'];
    var msg = 'La fecha límite se calcula automáticamente de acuerdo a la configuración de la operación. Duración mínima: ' + (wDays / 7) + ' semanas, duración máxima: ' + ((wMaxDays / 7)) + ' semanas.';
    $('#wndCreateCertification cite').attr('title', msg);
    if (appBosch.wndCreateCertificationStart === undefined) {
        appBosch.wndCreateCertificationStart = inputStart.kendoDatePicker({
            max: currentDate,
            value: currentDate,
            change: function () {
                var inputStart = $('input.cStart');
                var inputEnd = $('input.cEnd');
                var start = inputStart.data('kendoDatePicker');
                var startDate = start.value();
                var end = inputEnd.data('kendoDatePicker');
                var minDate = new Date(startDate);
                minDate.setDate(startDate.getDate() + wDays);
                var maxDate = new Date(startDate);
                maxDate.setDate(startDate.getDate() + wMaxDays);
                end.min(minDate);
                end.value(minDate);
                end.max(maxDate);
            }
        }).attr('readonly', 'readonly').data('kendoDatePicker');
    } else {
        appBosch.wndCreateCertificationStart.value(kendo.toString(currentDate, 'd'));
    }
    /// Fin de la certificación
    var minDate = new Date();
    minDate.setDate(minDate.getDate() + wDays);
    var maxDate = new Date();
    maxDate.setDate(maxDate.getDate() + wMaxDays);
    if (appBosch.wndCreateCertificationEnd === undefined) {
        appBosch.wndCreateCertificationEnd = inputEnd.kendoDatePicker({
            value: minDate,
            min: minDate,
            max: maxDate
        }).attr('readonly', 'readonly').data('kendoDatePicker');
    } else {
        appBosch.wndCreateCertificationEnd.value(kendo.toString(minDate, 'd'));
        appBosch.wndCreateCertificationEnd.min(kendo.toString(minDate, 'd'));
        appBosch.wndCreateCertificationEnd.max(kendo.toString(maxDate, 'd'));
    }

}